﻿using System;
using UnityEngine;
using UnityEngine.Audio;

// Token: 0x0200001C RID: 28
public class MusicManager : MonoBehaviour
{
	// Token: 0x0600007C RID: 124 RVA: 0x0000D814 File Offset: 0x0000BA14
	private void Start()
	{
		MusicManager.main = this;
		this.source = base.GetComponent<AudioSource>();
		if (!PlayerPrefs.HasKey("musicVol"))
		{
			PlayerPrefs.SetFloat("musicVol", 0.3f);
			this.source.clip = this.mainSongs[0];
		}
		else
		{
			this.source.clip = this.mainSongs[Random.Range(0, this.mainSongs.Length)];
		}
		this.curCutoff = 0f;
		this.source.volume = 0f;
		this.volume = PlayerPrefs.GetFloat("musicVol");
		this.source.Play();
	}

	// Token: 0x0600007D RID: 125 RVA: 0x0000D8BC File Offset: 0x0000BABC
	private void Update()
	{
		this.volume = Mathf.Clamp01(this.volume);
		this.desiredVolume = Mathf.Lerp(this.desiredVolume, this.volume, 2f * Time.unscaledDeltaTime);
		this.targetCutoff = Mathf.Lerp(this.targetCutoff, 22000f, 2f * Time.unscaledDeltaTime);
		this.recentDamage += this.prevHealth - this.body.health;
		this.recentDamage -= Time.deltaTime * 33f;
		this.recentDamage = Mathf.Clamp(this.recentDamage, 0f, 100f);
		if (this.fadeOut)
		{
			this.hankvolMult -= Time.deltaTime * 0.1f;
		}
		if (Input.GetKeyDown(KeyCode.K))
		{
			this.source.clip = this.mainSongs[Random.Range(0, this.mainSongs.Length)];
			this.source.Play();
		}
		if (this.body)
		{
			if (this.body.hitStun > 0f)
			{
				float num = Mathf.Lerp(this.volume, 0f, Mathf.Max(this.body.hitStun * 0.8f, this.recentDamage * 0.025f));
				if (this.desiredVolume > num)
				{
					this.desiredVolume = num;
				}
				float num2 = 22000f - Mathf.Max(this.body.hitStun * 22000f, this.recentDamage * 440f);
				if (num2 < 2000f)
				{
					num2 = 2000f;
				}
				if (this.targetCutoff > num2)
				{
					this.targetCutoff = num2;
				}
			}
			if (!this.body.alive)
			{
				this.desiredVolume = this.volume * 0.4f;
				this.targetCutoff = 2000f;
			}
		}
		this.source.volume = Mathf.Lerp(this.source.volume, this.desiredVolume * (1f - GameManager.main.fadeProgress * 1.5f) * this.hankvolMult, 4f * Time.unscaledDeltaTime);
		this.curCutoff = Mathf.Lerp(this.curCutoff, this.targetCutoff * (1f - GameManager.main.fadeProgress * 1.5f), 4f * Time.unscaledDeltaTime);
		this.mixer.SetFloat("Cutoff", this.curCutoff);
		if (!this.source.isPlaying && this.source.time > this.source.clip.length * 0.99f)
		{
			this.source.clip = this.mainSongs[Random.Range(0, this.mainSongs.Length)];
			this.source.Play();
		}
		if (!this.source.isPlaying)
		{
			this.timeNotPlaying += Time.deltaTime;
			if (this.timeNotPlaying > 2f)
			{
				this.source.clip = this.mainSongs[Random.Range(0, this.mainSongs.Length)];
				this.source.Play();
				this.timeNotPlaying = 0f;
			}
		}
		else
		{
			this.timeNotPlaying = 0f;
		}
		this.prevHealth = this.body.health;
	}

	// Token: 0x04000147 RID: 327
	public AudioClip[] mainSongs;

	// Token: 0x04000148 RID: 328
	[HideInInspector]
	public AudioSource source;

	// Token: 0x04000149 RID: 329
	private float desiredVolume = 1f;

	// Token: 0x0400014A RID: 330
	[HideInInspector]
	public float volume = 1f;

	// Token: 0x0400014B RID: 331
	public BodyScript body;

	// Token: 0x0400014C RID: 332
	public AudioMixer mixer;

	// Token: 0x0400014D RID: 333
	private float targetCutoff = 22000f;

	// Token: 0x0400014E RID: 334
	private float curCutoff = 22000f;

	// Token: 0x0400014F RID: 335
	public static MusicManager main;

	// Token: 0x04000150 RID: 336
	private float timeNotPlaying;

	// Token: 0x04000151 RID: 337
	private float recentDamage;

	// Token: 0x04000152 RID: 338
	private float prevHealth;

	// Token: 0x04000153 RID: 339
	public bool fadeOut;

	// Token: 0x04000154 RID: 340
	private float hankvolMult = 1f;
}
